package com.ruoyi.project.cms.stcal.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.stcal.domain.Stcal;
import com.ruoyi.project.cms.stcal.mapper.StcalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StcalServiceImpl implements StcalService {
    //声明mapper层属性
    @Autowired
    private StcalMapper stcalMapper;

    //删除公告类型
    @Override
    public int deleteTypeByIds(String ids) {
        Long[] typeIds = Convert.toLongArray(ids);

       /* for (Long typeId : typeIds)
        {
            //判断要删除的公告类型是否已经存在对应的公告信息
            *//*Post post = selectPostById(postId);
            if (countUserPostById(postId) > 0)
            {
                throw new BusinessException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }*//*
        }*/

        return stcalMapper.deleteTypeByIds(typeIds);
    }

    //保存修改公告类别
    @Override
    public int updateCmsType(Stcal stcal) {
        stcal.setPpName(ShiroUtils.getLoginName());
        return stcalMapper.updateCmsTypeMapper(stcal);
    }

    //获取要修改的公告信息
    @Override
    public Stcal selectTypeById(Long reId) {
        return stcalMapper.selectTypeByIdMapper(reId);
    }

    //公告类型的新增
    @Override
    public int insertCmsType(Stcal stcal) {
        stcal.setPpName(ShiroUtils.getLoginName());
        return stcalMapper.insertCmsType(stcal);
    }

    //校验类型名称
    @Override
    public String checkTypeNameUnique(Stcal stcal) {
        Long reId = StringUtils.isNull(stcal.getReId()) ? -1L : stcal.getReId();
        Stcal info = stcalMapper.checkTypeNameUnique(stcal.getPpName());
        if (StringUtils.isNotNull(info) && info.getReId().longValue() != reId.longValue()) {
            return UserConstants.TYPE_NAME_NOT_UNIQUE;
        }
        return UserConstants.TYPE_NAME_UNIQUE;
    }

    //加载公告类型信息
    @Override
    public List<Stcal> selectCmsTypeList(Stcal stcal) {

        return stcalMapper.selectCmsTypeListMapper(stcal);
    }
}




